package com.treasure.service.creditCard.service.impl;

import com.treasure.service.BaseService;
import com.treasure.service.creditCard.service.OrderService;
import com.treasure.service.dto.ApplyForDto;
import com.treasure.service.dto.TApplyCardOrderDto;
import com.treasure.service.third.utils.RestClient;
import com.treasure.service.result.PageResult;
import com.treasure.service.result.ResponseResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Service
public class OrderServiceImpl extends BaseService implements OrderService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private RestClient restClient;

    @Value("${treasure.rest.service.third}")
    private String thirdUrl;
    
	@Override
	public ResponseResult createApplyCardOrder(TApplyCardOrderDto applyCardOrder) {
		applyCardOrder.setChannel("91");
		return restClient.post(this.getGateway()+"/order/v1/createCardOrder",applyCardOrder,ResponseResult.class);

	}

	@Override
	public TApplyCardOrderDto getOrderMsg(Long orderId) {

		return null;

	}

	@Override
	public PageResult<List<TApplyCardOrderDto>> getCardOrderList(Integer pageSize, Integer pageNum, Long userId,String param) {

		MultiValueMap<String, Object> map = new LinkedMultiValueMap();
		map.set("pageSize",pageSize+"");
		map.set("pageNum",pageNum+"");
		map.set("userId",userId);
		map.set("param",param);

		HttpHeaders httpHeaders = new HttpHeaders();
		HttpEntity<MultiValueMap<String, Object>> formEntity = new HttpEntity<MultiValueMap<String, Object>>(map, httpHeaders);
		ParameterizedTypeReference<PageResult<List<TApplyCardOrderDto>>> pt = new ParameterizedTypeReference<PageResult<List<TApplyCardOrderDto>>>() {
		};
		try {
			ResponseEntity<PageResult<List<TApplyCardOrderDto>>> resp = restTemplate.exchange(this.getGateway() + "/order/v1/getCardOrderList",
					HttpMethod.POST, formEntity, pt);
			return resp.getBody();
		} catch (HttpClientErrorException e) {
			logger.error("请求方法 "+this.getGateway() + "/order/v1/getCardOrderList statusCode : {} error:{} ", e.getStatusCode(), e);
		}

		return null;
	}

	@Override
	public ResponseResult getCardOrderDetails(ApplyForDto applyForDto) {
		return restClient.post(this.getGateway()+"/creditCard/v1/queryCreditCardSchedule",applyForDto,ResponseResult.class);
	}

}
